package com.chenfan.magic.entity;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.chenfan.magic.utils.AES;
import com.chenfan.magic.utils.StringKit;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.io.Serializable;
import java.util.Date;
/**
 * @author lm
 */
@Data
@TableName("idc_scripts")
@ApiModel(value = "Scripts对象", description = "Scripts对象")
public class IdcScripts implements Serializable {

    private static final long serialVersionUID = 1L;

    private static final String ENCRYPT_KEY = "$62a51ed179969dd3af28434b2e967913";

    @TableId(value = "id", type = IdType.ID_WORKER_STR)
    @ApiModelProperty(value = "id")
    private String id;

    @ApiModelProperty(value = "创建者")
    private String createBy;

    @ApiModelProperty(value = "创建时间")
    private Date createTime;

    @ApiModelProperty(value = "更新者")
    private String updateBy;

    @ApiModelProperty(value = "更新时间")
    private Date updateTime;

    @ApiModelProperty(value = "备注信息")
    private String remarks;

    @ApiModelProperty(value = "应用 Key")
    private String appId;

    @ApiModelProperty(value = "脚本组 id")
    private String groupId;

    @ApiModelProperty(value = "脚本名称")
    private String name;

    @ApiModelProperty(value = "脚本描述")
    private String description;

    @ApiModelProperty(value = "脚本上下文")
    private String context;

    @ApiModelProperty(value = "请求REST方式")
    private String method;

    @ApiModelProperty(value = "元数据锁")
    private String metaLock;

    @ApiModelProperty(value = "排序")
    private int sort;

    @ApiModelProperty(value = "是否需要授权")
    private String isNeedToken;

    @ApiModelProperty(value = "状态")
    private String status;

    @ApiModelProperty(value = "首次发布时间")
    private Date publishTimeFirst;

    @ApiModelProperty(value = "最近发布时间")
    private Date publishTimeNearly;

    @ApiModelProperty(value = "累计发布次数")
    private int publishTimes;

    @ApiModelProperty(value = "最近发布版本号")
    private String publishVersion;

    @ApiModelProperty(value = "是否存在需要发布内容")
    private String publishSign;

    @ApiModelProperty(value = "发布模式")
    private String publishMode;

    @ApiModelProperty(value = "灰度用户列表")
    private String grayUnionIds;

    @ApiModelProperty(value = "执行次数")
    private String executeTimes;

    @ApiModelProperty(value = "最近执行时间")
    private Date executeTime;

    @ApiModelProperty(value = "令牌箭")
    private String tokenArrows;

    @ApiModelProperty(value = "预请求组")
    private String preRequestParams;

    @ApiModelProperty(value = "预响应组")
    private String preResponseParams;

    @ApiModelProperty(value = "文档")
    private String apiDocument;

    @ApiModelProperty(value = "效率分析报告")
    private String oeeReport;

    @ApiModelProperty(value = "语法分析报告")
    private String syntaxReport;

    @ApiModelProperty(value = "逻辑删除")
    private String logicDel;

    @ApiModelProperty(value = "路由")
    private String route;

    @ApiModelProperty(value = "发布者名称")
    private String publishBy;

    @ApiModelProperty(value = "定时表达式")
    private String scheduleCron;

    @ApiModelProperty(value = "索引")
    private String indez;

    @ApiModelProperty(value = "拦截器组")
    private String filters;

    @ApiModelProperty(value = "自定义标签")
    private String customStatus;

    @ApiModelProperty(value = "执行成功次数")
    private String executeSuccessTimes;

    @ApiModelProperty(value = "解析引擎")
    private String resolveEngine;

    @ApiModelProperty(value = "缓存开关")
    private Integer isCache;

    @ApiModelProperty(value = "缓存级别")
    private Integer isCacheLevel;

    @ApiModelProperty(value = "开发锁")
    private Long devLock;

    @ApiModelProperty(value = "拦截器标识")
    private Integer isFilter;

    public String getContextDecrypt(String sercet) {
        if (StringKit.isNotBlank(context) && context.trim().startsWith(ENCRYPT_KEY)) {
            context = AES.decrypt(context.replace(ENCRYPT_KEY, ""), sercet);
        }
        return context;
    }

    public void setContextEncrypt(String context, String sercet) {
        if (StringKit.isNotBlank(context) && !context.trim().startsWith(ENCRYPT_KEY)) {
            context = ENCRYPT_KEY + AES.encrypt(context, sercet);
        }
        this.context = context;
    }
}
